<?php


namespace app\admin\controller\cms;


use app\admin\controller\AuthController;
use app\admin\model\cms\CmsCategory as CModel;
use app\admin\model\cms\CmsArticle as AModel;
use app\admin\model\cms\CmsTag as TModel;
use app\Request;
use learn\services\UtilService as Util;
use FormBuilder\Factory\Elm;
// use GatewayWorker\Lib\Db;
use learn\services\FormBuilderService as Form;
use think\facade\Db;
use think\facade\Route as Url;

class CmsArticle extends AuthController
{
    /**
     * 文章
     * @param Request $request
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Exception
     */
    public function index($id=0)
    {
    
        $where = Util::postMore([
            ['name',""],
            ['status',""],
            ['id',""],
        ]);
        $this->assign("where",$where);
        $this->assign("list",AModel::systemPage($where));
        return $this->fetch();
    }

    /**
     * 添加
     * @param Request $request
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add($id=0)
    {
        // $this->assign(['title'=>'添加数据组','save'=>Url::buildUrl('save')]);
        // return $this->fetch();
        $form = array();
        $form[] = Elm::input('name','名称')->col(18);
        $form[] = Elm::radio('status','状态',1)->options([['label'=>'启用','value'=>1],['label'=>'禁用','value'=>0]])->col(18);
        $form =  Form::make_post_form($form, url('save',['id'=>$id])->build());
        $this->assign(compact('form'));
        return $this->fetch("public/form-builder");
    }

    /**
     * 修改
     * @param Request $request
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit($id=0)
    {
        if (!$id) return app("json")->fail("权限id不能为空");
        $info = AModel::get($id);
        if (!$info) return app("json")->fail("没有该权限");
        $form = array();
        $form[] = Elm::input('name','名称',$info['name'])->col(18);
        $form[] = Elm::radio('status','状态',$info['status'])->options([['label'=>'启用','value'=>1],['label'=>'禁用','value'=>0]])->col(18);
        $form = Form::make_post_form($form, url('update',['id'=>$id])->build());
        $this->assign(compact('form'));
        return $this->fetch("public/form-builder");
    }

    /**
     * 保存
     * @param int $id
     * @return void
     */
    public function save($id=0)
    {
        $data = Util::postMore([
            ['name',''],
            ['status',1],
        ]);
        if ($data['name'] == '') return app("json")->fail("名称不能为空");
          $count=  Db::name('cms_article')->where('cid',$id)->where('status',1)->count();
          if($count>=5){
            return app("json")->fail("组数达到上限");
          }
            $data['create_user'] = $this->adminId;
            $data['create_time'] = time();
            $data['cid'] = $id;
            $res = AModel::insert($data);
        return $res ? app("json")->success("操作成功",'code') : app("json")->fail("操作失败");
    }
    
    
        /**
     * 保存
     * @param int $id
     * @return void
     */
    public function update($id=0)
    {
        $data = Util::postMore([
            ['name',''],
            ['status',1],
        ]);
        if ($data['name'] == '') return app("json")->fail("名称不能为空");
            $data['update_user'] = $this->adminId;
            $data['update_time'] = time();
            $res = AModel::update($data,['id'=>$id]);
        return $res ? app("json")->success("操作成功",'code') : app("json")->fail("操作失败");
    }
}